package com.devhelper.examples.gef.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;

public class Node {
    private String name;
    private Rectangle layout;

    private List<Node> children;
    private Node parent;

    private PropertyChangeSupport listeners;

    public static final String PROPERTY_LAYOUT = "NodeLayout";

    public Node() {
        name = "unknown";
        layout = new Rectangle(10, 01, 100, 100);
        children = new ArrayList<Node>();
        parent = null;

        listeners = new PropertyChangeSupport(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Rectangle getLayout() {
        return layout;
    }

    public void setLayout(Rectangle newLayout) {
        Rectangle oldLayout = layout;
        layout = newLayout;

        getListeners().firePropertyChange(PROPERTY_LAYOUT, oldLayout, newLayout);
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public boolean addChild(Node child) {
        child.setParent(this);
        return children.add(child);
    }

    public boolean removeChild(Node child) {
        return children.remove(child);
    }

    public List<Node> getChildren() {
        return children;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        listeners.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        listeners.removePropertyChangeListener(listener);
    }

    public PropertyChangeSupport getListeners() {
        return listeners;
    }
}
